<?php session_start(); ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Experiment </title>
         <?php $_SESSION['page_name']='Your Choice Display';include('Menu.php');?>
           </head>
    <body> <form name="form1" action="deleteElement.php" method="post">
        
        <?php 
        //<input type='submit' name='t".$i."' value='delete Task' src='images/button1.png' />
        $con= mysql_connect("localhost","root","");
        if(!$con){
die('Connection Error!:' . mysql_error());
}
mysql_select_db ("usabilityevaluation",$con);
$_SESSION['id_Exp'];
echo "<table border='0.5'><tr><td>Experiment ID</td><td><input type='text' value='".$_SESSION['id_Exp']."' disabled='false'/></td><td>Add Task<a href='SetUp.php'><img id='image1' src='images/Add.png' /></a></td></tr>";
$Exp_id=$_SESSION['id_Exp'];
//$_SESSION['task_id'];

if ($_SESSION['name_exp']!=null){
//$resultOld = mysql_query("SELECT id_exp FROM experimentation WHERE Title='$oldExpName'") or die('Error, insert query failed');
//$oldExpName=$_SESSION['name_exp'];
//$NewExp_id=$Exp_id;
//DUplicate all record with new ID.....
//$query69="INSERT INTO task (id_task,Title,Duration,Comment,id_experiment) SELECT id_task,Title,Duration,Comment,$Exp_id 
//FROM task Where id_experiment=$_SESSION['Old_id']";
//$query70="INSERT INTO question (n_question,Title,Type,Comment,id_exp,id_task) SELECT n_question,Title,Type,Comment,$Exp_id,id_task FROM question Where id_exp=$_SESSION['Old_id']";
//mysql_query($query69);mysql_query($query70);
//$query68="SELECT id_question FROM question WHERE id_exp=$_SESSION['Old_id']";//to retrieve old id to join it to answers
//$query67="SELECT id_question FROM question WHERE id_exp=$Exp_id";//to retrieve new question id
//mysql_query($query68);mysql_query($query67);
 
 //while($row68 = mysql_fetch_array($query68)){//old ids

 //while($row67 = mysql_fetch_array($query67))
 // {
 // $query71="INSERT INTO answers (id_question,title,numero,selectedAnswer) SELECT $row67[0],title,numero,selectedAnswer FROM answers Where id_question=$rowOld[0] ";
//mysql_query($query71);
//  }
//}
}
else {
//Create Table

$query111="SELECT count(id_task) FROM task WHERE id_experiment=$Exp_id";
  $result111 =mysql_query($query111) or die('Error, retrieve query failed Question');
  while($row111 = mysql_fetch_array($result111)){$counter=$row111[0];}
for ($i=1;$i<=$counter;$i++){//Task Loop
echo "<tr><td>Task ID</td><td><input type='text' value='".$i."' disabled='false'/></td></tr>
<table border='1'>
    <tr>
    <th>Question ID</th>
    <th>Title</th>
    <th>Answer</th><th>Comment</th><td><input type='submit' name='t".$i."' value='delete Task'/></td>
    </tr>" ;
for($y=1;$y<=5;$y++){//Question Loop
$query2= "Select * from question where id_task='$i' and n_question=$y  and id_exp='$Exp_id'"; //this is the right line
//$query2= "Select * FROM question WHERE id_question='$y'and id_task='$i' and id_exp=67";
  $result2 =mysql_query($query2) or die('Error, retrieve query failed Question');
    
  $query3= "Select a.title from answers as a ,question as q where q.n_question=$y and q.id_exp=$Exp_id and a.id_question=q.id_question";
 // $query3= "Select * from answers Where id_question=$y and id_exp=$Exp_id";
  // $query3= "Select * from answers where id_question='$y' and id_task='$i' and id_exp=67";
      $result3 =mysql_query($query3) or die('Error, retreive Answer query failed');

     $cntr=1;// this counter to make the Radio button same name
while($row2 = mysql_fetch_array($result2))
  {//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   echo "<tr>";
   $Type=$row2['Type'];
     echo "<td>" . $row2['n_question'] . "</td>";
     echo "<td>" . $row2['Title'] . "</td><td> ";

     //Start Add Answer CheckBoxes
     if ($Type=='Multiple Choice'){
   while($row3 = mysql_fetch_array($result3)){
 echo  $row3[0] . "<input type='checkbox' name='".$row3[0]."' value='".$row3[0]."' />";
}
}
else if ($Type=='TrueFalse'){
 while($row3 = mysql_fetch_array($result3)){
 echo  $row3[0] . "<input type='radio' name='$cntr' value='".$row3[0]."' />";
}
}
else {
 while($row3 = mysql_fetch_array($result3)){
 echo  $row3[0] . "<textarea  type='text' name='".$row3[0]."' ></textarea >";
}
}

echo "</td>";
//finish ADD Answer
  echo "<td><textarea  value=".$row2['Comment']." ></textarea></td>";
   echo "<td><input type='submit' name='".$y."' value='delete Question' style='visibility:hidden;'/><td>Add Question<a href='questionSave.php'><img id='image1' src='images/Add.png' /></a></td></tr>";
  echo "</tr>";
  
  }
       $cntr++;
}
echo "</table><br/>";
} }
echo "</table><br/>";
//$_SESSION['id_Exp']=$NewExp_id;

mysql_close($con);
        ?>
        
 
   </form> </body>
</html>
